<?php
class Admin_RoleController extends Zend_Controller_Action
{
	public function indexAction()
	{
		
	}
	
	public function createAction()
	{
		$roleForm = new Admin_Form_Role();
		$roleForm->setAction('/admin/role/create')->setMethod('post');
		
		if($this->_request->isPost()) {
			if($roleForm->isValid($_POST))
			{
				$roleModel = new Admin_Model_Role();
				$roleModel->createRole(
					$roleForm->getValue('role_name'),
					$roleForm->getValue('description')
				);
				$this->_redirect('/admin/role/list');
			}
		}		
		$this->view->form = $roleForm;
	}
	
	public function updateAction()
	{
		$roleForm = new Admin_Form_Role();
		$roleForm->setAction('/admin/role/update')->setMethod('post');		

		if($this->_request->isPost()) {
			if($roleForm->isValid($_POST)) {
				$roleData = $roleForm->getValues(); 
				$roleModel = new Admin_Model_Role();				
				$roleModel->updateRole(
					$roleData['role_id'],
					$roleData['role_name'],
					$roleData['description']
				);
				$this->_redirect('/admin/role/list');
			}
		} else {
			$roleModel = new Admin_Model_Role();	
			$id = $this->_request->getParam('id'); 
			$roleRow = $roleModel->find($id)->current();
			$roleForm->populate($roleRow->toArray());
		}
		
		$this->view->form = $roleForm;
	}
	
	public function deleteAction()
	{
		$id = $this->_request->getParam('id');
		$roleModel = new Admin_Model_Role();
		$roleModel->deleteRole($id);
		$this->_redirect('/admin/role/list');
	}
	
	public function listAction()
	{
		$roleModel = new Admin_Model_Role();		
		$roleArray = $roleModel->getRoleArray();
		if(count($roleArray) > 0) {
			$paginator = Zend_Paginator::factory($roleArray);
			$page = $this->_request->getParam('page', 1);
			$paginator->setItemCountPerPage(5);
			$paginator->setCurrentPageNumber($page);			
			$this->view->paginator = $paginator;	
		}
	}
}